<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '轮播图添加' : '轮播图修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <el-form :model="user">
        
        <el-form-item label="标题" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <div>
            <div class="my-upload">
              <h3>+</h3>
              <img v-if="imgUrl" :src="imgUrl" alt="" />
              <input type="file" @change="getFile" />
            </div>
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqbanneradd, reqbanneredit, reqbannerinfo } from "../../../http/api";
import { formClose } from "../../../mixins";
import { successAlert } from "../../../utils/alert";
import path from "path";
export default {
  // 4.接收info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      // 5.1 初始化表单数据
      user: {
        title: "",
        img: null,
        status: 1,
      },
      imgUrl: "", //图片地址
    };
  },
  computed: {
    ...mapGetters({
      arr: "banner/arr",
    }),
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "banner/reqArr"
    }),

    //选了图片
    getFile(e) {
      let file = e.target.files[0];

      //验证文件大小
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }
      // 验证文件类型
      let extname = path.extname(file.name);
      let imgArr = [".png", ".jpg", ".jpeg", ".gif"];
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }

      //生成一张图
      this.imgUrl = URL.createObjectURL(file);

      //赋值给user.img
      this.user.img = file;
    },


    // 清空user
    empty() {
      this.user = {
        title: "",
        img: null,
        status: 1,
      };
      // this.$refs.upload.setImgUrl("");
      this.ref="";
    },
    // 5.4 点了添加
    add() {
      reqbanneradd(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.reqArr();
        }
      });
    },

    // 8. 获取一条数据
    getOne(id) {
      reqbannerinfo({ id: id }).then((res) => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.id = id;
          this.$refs.upload.setImgUrl(this.$pre + this.user.img);
        }
      });
    },
    // 9.修改
    update() {
      reqbanneredit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);

          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.reqArr();
        }
      });
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";

.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed black;
  position: relative;
  h3 {
    text-align: center;
    line-height: 100px;
  }
  input,
  img {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    cursor: pointer;
  }
  input {
    opacity: 0;
  }
}
</style>
